﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using WebSite.Core;
using Common;

namespace WebSite
{
    public partial class BabyGroup : BasePage
    {
        // public int currentTeamID = 0;
        public Maticsoft.Model.CarrefourCover.Baby babyA = null;
        public string babyAHeadImage = null;
        public string babyBHeadImage = null;
        public string babyALiveImage1 = null;
        public string babyALiveImage2 = null;
        public string babyBLiveImage1 = null;
        public string babyBLiveImage2 = null;
        public Maticsoft.Model.CarrefourCover.Baby babyB = null;

        public Maticsoft.Model.CarrefourCover.Team curTeam = null;

        public List<Maticsoft.Model.CarrefourCover.Message> listMessage = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            Maticsoft.BLL.CarrefourCover.Team teambll = new Maticsoft.BLL.CarrefourCover.Team();
            Maticsoft.BLL.CarrefourCover.Baby babybll = new Maticsoft.BLL.CarrefourCover.Baby();

            NavHelper.SetNavMenuSelectedIndex(1);
            NavHelper.SetNavStepCurrentStep(3);

            Project_Session.RefreshBabyInfo();

            if (Project_Session.CurrentUserBaby == null)
            {
                base.AlertMessageBack("决赛进行中，去给晋级宝宝帮投票吧");
                return;
            }
            
            /*if (Project_Session.CurrentUserBaby == null)
            {
                AlertMessage("您尚未填写宝宝资料", "/SubmitInfo.aspx");
            }*/
                       
            int curTeamID = NullableHelper.CastValueType(Project_Session.CurrentUserBaby.ConfirmTeamId);
            //如果未晋级，跳转
            var myTeam = new Maticsoft.BLL.CarrefourCover.Team().GetModel(curTeamID);
            if (myTeam != null)
            {
                var myType = myTeam.Type ?? 0;
                if (myType >= 2)
                {
                    Response.Redirect("ViewBabyGroup1112.aspx?teamid="+myTeam.Id.ToString());
                }
            };
            if (curTeamID == 0)
            {
                babyA = Project_Session.CurrentUserBaby;
            }
            else
            {
                curTeam = teambll.GetModel(curTeamID);
                if (curTeam == null)
                {
                    babyA = Project_Session.CurrentUserBaby;
                }
                else
                {
                    babyA = babybll.GetModel(curTeam.BabyAId.Value);
                    babyB = babybll.GetModel(curTeam.BabyBId.Value);

                    NavHelper.SetNavStepCurrentStep(6);
                }
            }

            // 获取生活照
            Maticsoft.BLL.CarrefourCover.Image imgbll = new Maticsoft.BLL.CarrefourCover.Image();
            babyAHeadImage = babyBHeadImage = babyALiveImage1 = babyALiveImage2 =
                babyBLiveImage1 = babyBLiveImage2 = "/images/BabyGroup/waitselect_200.png";

            if (babyA != null)
            {
                babyAHeadImage = babyA.BabyHeadImg;
                var liveImageA = imgbll.GetLiveImageByBabyID(babyA.Id);

                if (liveImageA.Count > 0)
                {
                    babyALiveImage1 = liveImageA[0].ImgUrl;
                }
                if (liveImageA.Count > 1)
                {
                    babyALiveImage2 = liveImageA[1].ImgUrl;
                }
            }

            if (babyB != null)
            {
                babyBHeadImage = babyB.BabyHeadImg;
                var liveImageB = imgbll.GetLiveImageByBabyID(babyB.Id);

                if (liveImageB.Count > 0)
                {
                    babyBLiveImage1 = liveImageB[0].ImgUrl;
                }
                if (liveImageB.Count > 1)
                {
                    babyBLiveImage2 = liveImageB[1].ImgUrl;
                }
            }

            if (curTeamID != 0)
            {
                Maticsoft.BLL.CarrefourCover.Message bllmsg = new Maticsoft.BLL.CarrefourCover.Message();
                listMessage = bllmsg.GetModelList("ReceviedTeamId=" + curTeamID).OrderByDescending(p => p.CreateTime).Take(100).ToList();
                // listMessage
            }
            else
            {
                listMessage = new List<Maticsoft.Model.CarrefourCover.Message>();
            }
        }
    }
}